$(document).ready(function(){
	
	$("div.Album").live("mouseenter", function(){
        
        var tooltipDelay = 800;
       
        $tooltip = $(this).children(".tooltip");           
        $tooltip.delay(tooltipDelay).queue(function(nxt) {
              $(this).addClass("tooltipHoverClass");
              nxt();
        });
    });
   
    $("div.Album").live("mouseleave", function(){
        $tooltip = $(this).children(".tooltip");           
        $tooltip.clearQueue().removeClass("tooltipHoverClass");
    });

	
	
	
        // Crear Tabs
        $( "#contentTabs" ).tabs();
        //$( "#header" ).tabs();
        
        // addTab button: just opens the dialog
        $( "#add_tab" )
            .button()
            .click(function() {
                $dialog.dialog( "open" );
        });
        // modal dialog init: custom buttons and a "close" callback reseting the form inside
        var $dialog = $( "#dialog" ).dialog({
            autoOpen: false,
            modal: true,
            width: '400px',
            resizable: false,
            buttons: {
                Add: function() {
                    addTab();
                    $( this ).dialog( "close" );
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            },
            open: function() {
                $('#tab_title').children().remove();
                var hiddenTabs = $('#tabsMenu li:hidden');
                hiddenTabs.each(function(){
                    var anchor = $(this).find('a');
                    $('#tab_title').append( $("<option></option>").attr("value",anchor.attr('href')).text(anchor.text()) );
                    //alert( $(this).find('a').attr('href') );
                });
                $('#tab_title').focus();
            },
            close: function() {
                $form[ 0 ].reset();
            }
        });
        // addTab form: calls addTab function on submit and closes the dialog
        var $form = $( "form", $dialog ).submit(function() {
            addTab();
            $dialog.dialog( "close" );
            return false;
        });
        
        $( "#about" )
	        .button()
	        .click(function() {
	        	$dialogAbout.dialog( "open" );
	    });
        
        var $dialogAbout = $( "#dialogAbout" ).dialog({
            autoOpen: false,
            modal: true,
            width: '400px',
            resizable: false,
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                }
            },
            open: function() {
                
            },
            close: function() {
                
            }
        });

        // actual addTab function: adds new tab using the title input from the form above
        function addTab() {
            //alert( $('#tab_title option:selected').val() );
            var href = $('#tab_title option:selected').val();
            $('a[href='+href+']').parent().css('display','block');
            $('a[href='+href+']').click();
        }
        
        // Crear acordiones
        /*
        $('.tabSeccion h3').click(function() {
            $(this).next().slideToggle('slow');
            return false;
        }).next().hide();*/
        
     
        /*$('.tabSeccion h3').collapsible({
            defaultOpen: 'current',
            cookieName: 'navAct',
            cssOpen: 'subOpened',
            cssClose: 'subClosed',
            speed: 200
        });*/
        
        
        /*$('#openAll').bind('click', function(){
        	$('.tabSeccion h3').collapsible('open');        	        	
        });*/
        
        $( "#refresh" )
        .button()
        .click(function() {
        	/*
        	var form = $('#jmlForm');
        	form.action='/refresh';
        	form.submit();
        	*/
        	window.location = 'refresh';
        });
        
        $( "#closeAll" )
        .button()
        .click(function() {
        	$('.tabSeccion h3').collapsible('close');
//        	$('.tabSeccion h3').collapsible().close();
        });
                
        // Ocultar Tabs
        $( "#tabsMenu span.ui-icon-close" ).live( "click", function() {
            var tabs = $("#contentTabs");
            //var tabs = $("#header");
            var index = $( "li", tabs ).index( $( this ).parent() );
            var tab = $('#tabsMenu li').get(index);
            $(tab).css('display', 'none');
            if ( $(tab).hasClass('ui-tabs-selected') )
            {
                $(tab).removeClass('ui-tabs-selected');
                $(tab).removeClass('ui-state-active');    
            }
            
            var anchor = $(tab).find('a');
            var tabSeccion = $( anchor.attr('href') );
            if (!tabSeccion.hasClass('ui-tabs-hide'))
                tabSeccion.addClass('ui-tabs-hide');
            
        });        
        
    });

/* 
function playMusic(idAlbum)
{
    var params = {
        'idAlbum':idAlbum    
    };
    
    ajaxRequest('playAlbum.jsp',params, playMusicCallBack);
}
*/

function playMusic(folder)
{
    var params = {
		'cmd':'play',
		'idAlbum':folder
    };
    
    ajaxGetRequest('player',params, null);
}

function playMusicCallBack(respuesta)
{
    if(respuesta.status == 'OK')
    {
        alert(respuesta.song);
        //alert(respuesta.hernan);
    }
}

function editAlbum(folder)
{
    var params = {
        'folder':folder    
    };
    
    ajaxGetRequest('edit',params, null);
}

function addToPlayList(folder)
{
    var params = {
    	'cmd':'add',
    	'idAlbum':folder
    };
    
    ajaxGetRequest('player',params, null);
}

function playAlbum(folder)
{
    var params = {
		'cmd':'play',
		'idAlbum':folder
    };
    
    ajaxGetRequest('player',params, null);
}

function exploreAlbum(folder)
{
    var params = {
        'path':folder    
    };
    
    ajaxGetRequest('explore',params, null);
}

function slideShowAlbum(folder)
{
 
	var params = {
        'path':folder    
    };
    
    ajaxGetRequest('slideshow',params, null);
}

function loadCovers(label, filterName, labelName)
{
	var labelComponent = $(label);
	var labelContainer = labelComponent.parent();
	var secccionContent = labelContainer.find('.secccionContent');  
	
	if(secccionContent.length == 0)
	{
		var params = {
		        'filterName':filterName,
		        'labelName':labelName
		    };
		
		//ajaxRequest('zShowCovers.jsp', params, function(response){ showLoadCover(label, response); } );
		ajaxRequest('zShowCovers.jsp', params, function(response){ showLoadCover(label, response); } );
	}
		
}

function showLoadCover(label, response)
{
	var labelComponent = $(label);
	var labelContainer = labelComponent.parent();
	
	labelContainer.append(response.covers);
	
	labelComponent.collapsible({
        defaultOpen: 'current',
        cookieName: 'navAct',
        cssOpen: 'subOpened',
        cssClose: 'subClosed',
        speed: 200
    });
	
	labelComponent.click();
}

function openArtist(tabName, artistName)
{
	$('a[href=#'+ tabName +']').click();
	
	var label = $('.tabSeccion h3[title="'+artistName+'"]');
	
	if(!label.hasClass('subOpened'))
	{
		label.click();
	}
	
	var top = label.offset().top - 150;
	
	$('html, body').animate({
		scrollTop: top 
	}, 2000);
}